<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:ddl="livcos.org/ns/db/ddl"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:sd="livcos.org/ns/design/system"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:v="livcos.org/ns/view"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="c doc ddl pm sd f v xs">
  
	<xsl:import href="base.xsl"/>
	<xsl:import href="lookup.xsl"/>
	
	<xsl:template match="ddl:table" mode="ddl_content">
		<table class="table_overview">
			<thead>
				<tr>
					<th>Feld</th>
					<th>Type</th>
					<th>Dimension</th>
					<th>Default</th>
					<th>Shortcut</th>
				</tr>
			</thead>
			<tbody>
				<xsl:apply-templates select="ddl:field" mode="ddl_table_overview">
					<xsl:sort select="@name = ../ddl:key/ddl:field/@ref-name" order="descending"/>
					<xsl:sort select="lower-case(@name)"/>
				</xsl:apply-templates>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="5" class="table_legend">
						<span class="primaryKey">&#160;&#160;</span>
						<xsl:text> Primary Key&#160;&#160; </xsl:text>
						<span class="mandatory">&#160;&#160;</span>
						<xsl:text> Mandatory&#160;&#160; </xsl:text>
						<span class="nullable">&#160;&#160;</span>
						<xsl:text> Optional (allow NULL)</xsl:text>
					</td>
				</tr>
			</tfoot>
		</table>
		<xsl:apply-templates select="ddl:key[ddl:referer]"/>
		<xsl:call-template name="ddl_doc"/>
		<v:chapter>
			<v:title>Felder</v:title>
			<xsl:apply-templates select="ddl:field" mode="ddl_table">
				<xsl:sort select="@name = ../ddl:key/ddl:field/@ref-name" order="descending"/>
				<xsl:sort select="lower-case(@name)"/>
			</xsl:apply-templates>
		</v:chapter>
		<xsl:apply-templates select="ddl:lookup"/>
		<xsl:call-template name="ddl_constraints"/>
		<xsl:if test="ddl:trigger">
			<v:chapter>
				<v:title>Triggers</v:title>
				<xsl:apply-templates select="ddl:trigger"/>
			</v:chapter>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="ddl:field" mode="ddl_table_overview">
    <tr>
      <xsl:attribute name="class">
        <xsl:choose>
          <xsl:when test="../ddl:key/ddl:field[@ref-name = current()/@name]">primaryKey</xsl:when>
          <xsl:when test="@nullable">nullable</xsl:when>
          <xsl:otherwise>mandatory</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <td>
        <a href="#{@id}"><xsl:value-of select="@name"/></a>
      </td>
      <td align="center">
        <xsl:value-of select="@dbtype" />
      </td>
      <td align="center">
      	<xsl:apply-templates select="@size"/>
      	<xsl:if test="not(@size)">&#160;</xsl:if>
      </td>
      <td>
        <xsl:if test="@type != 'varchar'">
          <xsl:attribute name="align">right</xsl:attribute>
        </xsl:if>
        <xsl:value-of select="@default"/><xsl:if test="not(@default)">&#160;</xsl:if>
      </td>
      <td>
        <xsl:variable name="refs" select="../ddl:ref[ddl:field/@ref-name = current()/@name]"/>
        <xsl:apply-templates select="$refs" mode="shortcut"/>
        <xsl:if test="not($refs)">&#160;</xsl:if>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="ddl:field" mode="ddl_table">
  	<v:chapter id="{@id}">
  		<v:title>
  			<xsl:call-template name="ddl-search">
  				<xsl:with-param name="label" select="string(@name)"/>
  				<xsl:with-param name="query" select="concat(@name, ' AND ', ../@name)"/>
  			</xsl:call-template>
  		</v:title>
  		<xsl:apply-templates select="*"/>
    	<p class="block">
    		<xsl:text>Einträge für das </xsl:text>
    		<xsl:if test="@nullable">optionale</xsl:if> Feld '<xsl:value-of select="@name"/>
    		<xsl:text>' vom Typ </xsl:text>
    		<xsl:apply-templates select="." mode="ddl_type"/>
        <xsl:choose>
          <xsl:when test="../ddl:key/ddl:field[@ref-name = current()/@name]"> bilden den Primary-Key der Tabelle.</xsl:when>
          <xsl:when test="@default"> werden mit <span class="text_value">
              <xsl:value-of select="@default"/>
            </span> als Defaultwert erstellt.</xsl:when>
          <xsl:otherwise> werden ohne Defaultwert erstellt.</xsl:otherwise>
        </xsl:choose>
      </p>
      <xsl:apply-templates select="../ddl:ref[ddl:field/@ref-name = current()/@name]"/>
    </v:chapter>
  </xsl:template>

	<xsl:template match="ddl:key[ddl:referer]">
		<v:chapter>
			<v:title>Referenzierende Tabellen</v:title>
			<p class="block">
				<xsl:for-each-group select="ddl:referer" group-by="@ref-table">
					<xsl:if test="position() > 1">, </xsl:if>
					<a href="{f:cosmos-uri(@ref-table, .)/obj/@name}.html">
						<xsl:value-of select="@ref-table-name"/>
					</a>
				</xsl:for-each-group>
			</p>
		</v:chapter>
	</xsl:template>

	<xsl:template match="ddl:ref" mode="shortcut">
  	<a href="{f:cosmos-uri(@key-table, .)/obj/@name}.html"><xsl:value-of select="@key-table-name"/></a>
  </xsl:template>
  
  <xsl:template match="ddl:ref">
  	<xsl:variable name="tbl" select="doc(@key-table)/*"/>
  	<xsl:choose>
  		<xsl:when test="$tbl/ddl:lookup">
  			<xsl:apply-templates select="$tbl/ddl:lookup" mode="lookup">
  				<xsl:with-param name="tableName" select="../@name"/>
  				<xsl:with-param name="fieldName" select="ddl:field[1]/@ref-name"/>
  			</xsl:apply-templates>
  		</xsl:when>
  		<xsl:otherwise>
  			<p class="block">
  				Das Feld referenziert einen Record aus der 
  				<a href="{f:cosmos-uri(@key-table, .)/obj/@name}.html">
  					<xsl:value-of select="@key-table-name"/>
  				</a> Tabelle.
  				<xsl:if test="@update | @delete">
  					Die referenzielle Integrität wird für
  					<xsl:choose>
  						<xsl:when test="@update and @delete">Updates und Deletes</xsl:when>
  						<xsl:when test="@update">Updates</xsl:when>
  						<xsl:when test="@delete">Deletes</xsl:when>
  					</xsl:choose>
  					<xsl:text> automatisch geprüft</xsl:text>
  					<xsl:variable name="cascade-action" as="xs:string?">
  						<xsl:choose>
  							<xsl:when test="(@update = 'cascade') and (@delete = 'cascade')">Updates und Deletes</xsl:when>
  							<xsl:when test="@update = 'cascade'">Updates</xsl:when>
  							<xsl:when test="@delete = 'cascade'">Deletes</xsl:when>
  						</xsl:choose>
  					</xsl:variable>
  					<xsl:if test="$cascade-action">
  						<xsl:text> (</xsl:text>
  						<xsl:value-of select="$cascade-action"/>
  						<xsl:text> werden propagiert)</xsl:text>
  					</xsl:if>
  					<xsl:text>.</xsl:text>
  				</xsl:if>
  			</p>
  		</xsl:otherwise>
  	</xsl:choose>
  </xsl:template>
	
	<xsl:template match="ddl:trigger">
		<v:chapter id="{@id}">
			<v:title path="title"><xsl:value-of select="@name"/></v:title>
			<xsl:apply-templates select="*"/>
		</v:chapter>
	</xsl:template>
	
</xsl:stylesheet>
